home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / msysjour / vol05 / 02 / checker3 / ckrboard.c < prev    next >
C/C++ Source or Header  |  1990-03-19  |  11KB  |  301 lines

  1. /*---------------------------------------------------------------
  2.    CKRBOARD.C -- BoardWndProc for user interaction, Version 0.30
  3.                  (c) 1990, Charles Petzold
  4.   ---------------------------------------------------------------*/
  5.  
  6. #define INCL_WIN
  7. #include <os2.h>
  8. #include <stdlib.h>
  9. #include "checkers.h"
  10. #include "ckrdraw.h"
  11.  
  12. MRESULT EXPENTRY BoardWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  13.      {
  14.      static BOOL     fMovingPiece ;
  15.      static HPS      hps ;
  16.      static HPOINTER hptrUpHand, hptrDnHand, hptrArrow ;
  17.      static HWND     hwndJudge ;
  18.      static POINTL   ptlLast ;
  19.      static SHORT    sBottom = BLACK, sColor = -1, sKing = 0 ;
  20.      BOARD           brd ;
  21.      POINTL          ptlMouse ;
  22.      SHORT           x, y, i ;
  23.  
  24.      switch (msg)
  25.           {
  26.           case WM_CREATE:
  27.                hps = CkdCreatePS (hwnd) ;
  28.  
  29.                hptrUpHand = WinLoadPointer (HWND_DESKTOP, NULL, IDP_UPHAND) ;
  30.                hptrDnHand = WinLoadPointer (HWND_DESKTOP, NULL, IDP_DNHAND) ;
  31.                hptrArrow  = WinQuerySysPointer (HWND_DESKTOP, SPTR_ARROW,
  32.                                                 FALSE) ;
  33.                return 0 ;
  34.  
  35.           case WM_SIZE:
  36.                CkdResizePS (hps, hwnd) ;
  37.                CkdDestroyPieces () ;
  38.                CkdCreatePieces (hps) ;
  39.                return 0 ;
  40.  
  41.           case WM_TELL_BOARD_JUDGE_HANDLE:
  42.                hwndJudge = HWNDFROMMP (mp1) ;
  43.                return 0 ;
  44.  
  45.           case WM_TELL_BOARD_NEW_ORIENTATION:
  46.                sBottom = SHORT1FROMMP (mp1) ;
  47.                WinInvalidateRect (hwnd, NULL, FALSE) ;
  48.                return 0 ;
  49.  
  50.           case WM_TELL_BOARD_COLOR_DIALOG:
  51.                if (!WinDlgBox (HWND_DESKTOP, hwnd, ColorDlgProc,
  52.                                NULL, IDD_COLOR_DLG, mp1))
  53.                     return 0 ;
  54.  
  55.                if (SHORT1FROMMP (mp2))
  56.                     {
  57.                     CkdDestroyPieces () ;
  58.                     CkdCreatePieces (hps) ;
  59.                     }
  60.                WinInvalidateRect (hwnd, NULL, FALSE) ;
  61.                return 0 ;
  62.  
  63.           case WM_TELL_BOARD_STANDARD_COLORS:
  64.                CkdSetStandardColors () ;
  65.                CkdDestroyPieces () ;
  66.                CkdCreatePieces (hps) ;
  67.                WinInvalidateRect (hwnd, NULL, FALSE) ;
  68.                return 0 ;
  69.  
  70.           case WM_BUTTON1UP:
  71.                if (sColor == -1)
  72.                     return 0 ;
  73.  
  74.                WinSetActiveWindow (HWND_DESKTOP, hwnd) ;
  75.  
  76.                                         // get mouse coords and index
  77.  
  78.                ptlMouse.x = MOUSEMSG(&msg)->x ;
  79.                ptlMouse.y = MOUSEMSG(&msg)->y ;
  80.                CkdQueryHitCoords (hps, ptlMouse, &x, &y) ;
  81.                i = CkdConvertCoordsToIndex (x, y, sBottom) ;
  82.  
  83.                if (i == -1)             // didn't hit black square
  84.                     {
  85.                     WinAlarm (HWND_DESKTOP, WA_ERROR) ;
  86.                     return 0 ;
  87.                     }
  88.  
  89.                if (!fMovingPiece)       // ie, picking up piece
  90.                     {
  91.                     if (!WinSendMsg (hwndJudge, WM_QUERY_JUDGE_PICKUP_PIECE,
  92.                                      MPFROMSHORT (i), NULL))
  93.                          {
  94.                          WinAlarm (HWND_DESKTOP, WA_ERROR) ;
  95.                          return 0 ;
  96.                          }
  97.  
  98.                     sKing = (SHORT) WinSendMsg (hwndJudge,
  99.                                         WM_QUERY_JUDGE_IF_KING, NULL, NULL) ;
  100.  
  101.                               // Remove the mouse pointer
  102.  
  103.                     WinSetPointer (HWND_DESKTOP, NULL) ;
  104.  
  105.                               // Erase, save area, and show piece at mouse
  106.  
  107.                     CkdErasePiece (hps, x, y) ;
  108.                     CkdDragSave (hps, &ptlMouse, sKing) ;
  109.                     CkdDragShow (hps, &ptlMouse, sColor, sKing) ;
  110.  
  111.                               // Prepare for WM_MOUSEMOVE
  112.  
  113.                     fMovingPiece = TRUE ;
  114.                     ptlLast = ptlMouse ;
  115.                     }
  116.  
  117.                else           // ie, attempt to set down piece
  118.                     {
  119.                     if (!WinSendMsg (hwndJudge, WM_QUERY_JUDGE_PUTDOWN_PIECE,
  120.                                      MPFROMSHORT (i), NULL))
  121.                          {
  122.                          WinAlarm (HWND_DESKTOP, WA_ERROR) ;
  123.                          return 0 ;
  124.                          }
  125.                               // restore area
  126.  
  127.                     CkdDragRestore (hps, &ptlMouse, sKing) ;
  128.  
  129.                     sKing = (SHORT) WinSendMsg (hwndJudge,
  130.                                         WM_QUERY_JUDGE_IF_KING, NULL, NULL) ;
  131.  
  132.                               // set down the piece on the square
  133.  
  134.                     CkdDragDeposit (hps, x, y, sColor, sKing) ;
  135.  
  136.                               // check for continued jumps
  137.  
  138.                     if (WinSendMsg (hwndJudge, WM_QUERY_JUDGE_CONTINUE_MOVE,
  139.                                     MPFROMSHORT (i), NULL))
  140.                          {
  141.                          CkdErasePiece (hps, x, y) ;
  142.                          CkdDragSave (hps, &ptlLast, sKing) ;
  143.                          }
  144.                     else           // the move is over
  145.                          {
  146.                          fMovingPiece = FALSE ;
  147.                          sColor       = -1 ;
  148.                          WinSetPointer (HWND_DESKTOP, hptrArrow) ;
  149.                          WinSendMsg (hwndJudge, WM_TELL_JUDGE_BOARD_MOVE_ENDED,
  150.                                      NULL, NULL) ;
  151.                          }
  152.                     }
  153.  
  154.                return 0 ;
  155.  
  156.           case WM_MOUSEMOVE:
  157.                ptlMouse.x = MOUSEMSG(&msg)->x ;
  158.                ptlMouse.y = MOUSEMSG(&msg)->y ;
  159.  
  160.                               // set the mouse pointer and move the piece
  161.  
  162.                if (fMovingPiece)
  163.                     {
  164.                     WinSetPointer (HWND_DESKTOP, NULL) ;
  165.                     CkdDragMove (hps, &ptlLast, &ptlMouse, sColor, sKing) ;
  166.                     ptlLast = ptlMouse ;
  167.                     }
  168.  
  169.                else if (sColor == -1)
  170.                     WinSetPointer (HWND_DESKTOP, hptrArrow) ;
  171.  
  172.                else
  173.                     WinSetPointer (HWND_DESKTOP,
  174.                          sBottom ^ sColor ? hptrDnHand : hptrUpHand) ;
  175.  
  176.                return 0 ;
  177.  
  178.           case WM_SETFOCUS:
  179.                               // set the mouse pointer
  180.  
  181.                if (WinQuerySysValue (HWND_DESKTOP, SV_MOUSEPRESENT) == 0)
  182.                     WinShowPointer (HWND_DESKTOP,
  183.                                     SHORT1FROMMP (mp2) ? TRUE : FALSE) ;
  184.  
  185.                if (fMovingPiece)
  186.                     WinSetPointer (HWND_DESKTOP, NULL) ;
  187.  
  188.                else if (sColor == -1)
  189.                     WinSetPointer (HWND_DESKTOP, hptrArrow) ;
  190.  
  191.                else
  192.                     WinSetPointer (HWND_DESKTOP,
  193.                          sBottom ^ sColor ? hptrDnHand : hptrUpHand) ;
  194.                return 0 ;
  195.  
  196.           case WM_CHAR:
  197.                if (CHARMSG(&msg)->fs & KC_KEYUP)
  198.                     return 0 ;
  199.  
  200.                if (!(CHARMSG(&msg)->fs & KC_VIRTUALKEY))
  201.                     return 0 ;
  202.  
  203.                          // convert pointer position to x, y coords
  204.  
  205.                WinQueryPointerPos (HWND_DESKTOP, &ptlMouse) ;
  206.                WinMapWindowPoints (HWND_DESKTOP, hwnd, &ptlMouse, 1) ;
  207.                CkdQueryNearestXYFromPoint (hps, &ptlMouse, &x, &y) ;
  208.  
  209.                          // move the coordinates
  210.  
  211.                switch (CHARMSG(&msg)->vkey)
  212.                     {
  213.                     case VK_HOME:      x = 0 ;  y = 7 ;  break ;
  214.                     case VK_END:       x = 0 ;  y = 0 ;  break ;
  215.                     case VK_PAGEUP:    x = 7 ;  y = 7 ;  break ;
  216.                     case VK_PAGEDOWN:  x = 7 ;  y = 0 ;  break ;
  217.  
  218.                     case VK_UP:        y = min (y + 1, 7) ;  break ;
  219.                     case VK_DOWN:      y = max (y - 1, 0) ;  break ;
  220.                     case VK_RIGHT:     x = min (x + 1, 7) ;  break ;
  221.                     case VK_LEFT:      x = max (x - 1, 0) ;  break ;
  222.  
  223.                     case VK_SPACE:     break ;
  224.  
  225.                     default:           return 0 ;
  226.                     }
  227.                          // process keystrokes like mouse messages
  228.  
  229.                CkdQuerySlightOffsetFromXY (hps, x, y, &ptlMouse) ;
  230.  
  231.                switch (CHARMSG(&msg)->vkey)
  232.                     {
  233.                     case VK_SPACE:
  234.                          WinSendMsg (hwnd, WM_BUTTON1UP,
  235.                                      MPFROM2SHORT ((SHORT) ptlMouse.x,
  236.                                                    (SHORT) ptlMouse.y), NULL) ;
  237.                          break ;
  238.  
  239.                     default:
  240.                          WinMapWindowPoints (hwnd, HWND_DESKTOP, &ptlMouse, 1) ;
  241.                          WinSetPointerPos (HWND_DESKTOP, (SHORT) ptlMouse.x,
  242.                                                          (SHORT) ptlMouse.y) ;
  243.                          break ;
  244.                     }
  245.                return 0 ;
  246.  
  247.           case WM_JUDGE_SAYS_REMOVE_PIECE:
  248.                i = SHORT1FROMMP (mp1) ;
  249.                CkdConvertIndexToCoords (i, &x, &y, sBottom) ;
  250.                CkdErasePiece (hps, x, y) ;
  251.                return 0 ;
  252.  
  253.           case WM_JUDGE_SAYS_RESET_BOARD:
  254.                fMovingPiece = FALSE ;
  255.                sColor = -1 ;
  256.                WinSetPointer (HWND_DESKTOP, hptrArrow) ;
  257.                WinInvalidateRect (hwnd, NULL, FALSE) ;
  258.                return 0 ;
  259.  
  260.           case WM_JUDGE_SAYS_MOVE_BLACK:
  261.                sColor = BLACK ;
  262.                WinSetPointer (HWND_DESKTOP,
  263.                               sBottom == BLACK ? hptrUpHand : hptrDnHand) ;
  264.                return 0 ;
  265.  
  266.           case WM_JUDGE_SAYS_MOVE_WHITE:
  267.                sColor = WHITE ;
  268.                WinSetPointer (HWND_DESKTOP,
  269.                               sBottom == BLACK ? hptrDnHand : hptrUpHand) ;
  270.                return 0 ;
  271.  
  272.           case WM_PAINT:
  273.                WinBeginPaint (hwnd, hps, NULL) ;
  274.  
  275.                WinSendMsg (hwndJudge, WM_QUERY_JUDGE_CURRENT_BOARD,
  276.                            MPFROMP (&brd), NULL) ;
  277.  
  278.                CkdDrawWindowBackground (hps, hwnd) ;
  279.                CkdDrawWholeBoard (hps) ;
  280.                CkdDrawAllPieces (hps, &brd, sBottom) ;
  281.  
  282.                if (fMovingPiece)
  283.                     {
  284.                     WinQueryPointerPos (HWND_DESKTOP, &ptlMouse) ;
  285.                     WinMapWindowPoints (HWND_DESKTOP, hwnd, &ptlMouse, 1) ;
  286.                     CkdDragSave (hps, &ptlMouse, sKing) ;
  287.                     CkdDragShow (hps, &ptlMouse, sColor, sKing) ;
  288.  
  289.                     ptlLast = ptlMouse ;
  290.                     }
  291.                WinEndPaint (hps) ;
  292.                return 0 ;
  293.  
  294.           case WM_DESTROY:
  295.                CkdDestroyPieces () ;
  296.                CkdDestroyPS (hps) ;
  297.                return 0 ;
  298.           }
  299.      return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
  300.      }
  301.